package com.flashflexpro.flexdirector.adapter.codec {
import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;
import com.flashflexpro.flexdirector.model.CodeStringValue;

import mx.automation.IAutomationObject;
import mx.automation.IAutomationObject;

public class CodecDateRange extends CodecDefault{

    override public function encode(obj:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject ):CodeStringValue{
        var rt:CodeStringValue = new CodeStringValue();
        rt.desc = descProperty.getDto();
        var val:Object = getMemberFromObject(obj, descProperty);

        if (val != null){
            if (val is Date){
                rt.value = rt.string = (val as Date).toLocaleDateString();
            }
            else if (val is Object){
                if ("rangeStart" in val)
                    rt.value = rt.string = (val.rangeStart as Date).toLocaleDateString();
                rt.value += " => "
                rt.string += " => "
                if ("rangeEnd" in val){
                    rt.value += (val.rangeEnd as Date).toLocaleDateString();
                    rt.string += (val.rangeEnd as Date).toLocaleDateString();

                }
            }
        	rt.code = "\"" + rt.string + "\"";
        }
        else{
        	rt.value = null;
        	rt.string = null;
        	rt.code = "null"
        }
        return rt;
    }

    override public function decode(obj:Object, value:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject, inString:Boolean = false):void {
        throw new Error("Resource not Settable");
    }
}
}